import router from '@/router'

import store from '@/store'

import NProgress from 'nprogress'
/* import {Message} from 'element-ui' */

import { getToken, getCookie } from '@/utils/cookie'

const whiteList = ['/login'] // 验权
const getRouterList = (to, from, next, res) => {
  // 获取跳转路由
  let toPathMeet = getCookie('toPathMeetORV')
  if (!toPathMeet) {
    toPathMeet = to.path
  }
  // 路由名称
  let namePath = ''
  // 是否存在此路由
  let is = false
  // 确认是否有此路由
  const rouPage = (rou, toPath) => {
    if (rou && rou.length > 0) {
      rou.forEach(item => {
        if (item.path === toPath) {
          is = true
          namePath = item.name
        } else {
          if (item.children) {
            rouPage(item.children, toPath)
          }
        }
      })
    }
  }
  // 没有指定跳转路由时 默认第一条
  const returnToPath = (pathData) => {
    let result = '/login'
    const findPath = (p) => {
      if (p.children && p.children.length > 0) {
        findPath(p.children[0])
      } else {
        namePath = p.name
        result = p.path
      }
    }
    if (pathData.length > 0) {
      findPath(pathData[0])
    }
    return result
  }
  if (res.length > 0) { // 如果有路由信息
    // 去处理路由信息
    if (store.getters.addRouters.length === 0) {
      store.dispatch('GenerateRoutes', res).then((routers) => {
        router.addRoutes(routers) // 动态添加可访问路由表
        console.log(store.getters.addRouters)
        // router.options.routes = store.getters.routers
        // 是否存在当前要跳转的路由
        rouPage(routers, toPathMeet)
        if (!is) { // 不存在 去获取有效路由的第一条
          toPathMeet = returnToPath(routers)
        }
        if (toPathMeet && namePath) {
          next({ path: toPathMeet, name: namePath })
        } else {
          if (is && from.path !== '/login') {
            next({ ...to, replace: true })
          } else {
            next('/login')
          }
        }
      })
    } else {
      next()
    }
  } else {
    // 如果没有路由信息
    store.dispatch('FedLogOut').then(() => {
      next(`/login`)
    })
  }
}

router.beforeEach((to, from, next) => {
  console.log('------------------beforeEach')
  NProgress.start()
  const username = store.getters.username || getCookie('username')
  //  console.log(to.path)
  // 如果没有token和名称定向到login
  if (getToken() && username) {
    // const username = store.getters.username || getCookie('username')
    const menus = JSON.parse(localStorage.getItem('menus'))
    // 判断是否有菜单信息
    if (menus && menus.length > 0) {
      getRouterList(to, from, next, menus)
      // next()
    } else {
      store.dispatch('getInfo', username).then(res => {
        getRouterList(to, from, next, res)
      })
    }
    NProgress.done()
  } else {
    store.dispatch('FedLogOut')
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next(`/login`) // 否则全部重定向到登录页
    }
    NProgress.done()
  }
})

router.afterEach(() => {
  NProgress.done() // 结束Progress
})

/* router.beforeEach((to, from, next) => {

  NProgress.start()
  if (whiteList.indexOf(to.path) !== -1) {
    next()
  } else {
    next()
  }
})
router.afterEach(() => {
  NProgress.done()
}) */
